package org.lhz.controller;

import com.alibaba.fastjson.JSON;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@WebServlet("/file")
public class FileUploadAndDownload extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        switch(method){
            case"uploadFile":
                uploadFile(req,resp);
                break;
            case"downloadShowFile":
                downloadShowFile(req,resp);
                break;
        }
    }
    /**
     * Infor: 上传文件
     * @param req
     * @param resp
     * @return : void
     * @author : LHZ
     * @date : 2021/11/6 17:04
     */
    private void uploadFile(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        //获取请求参数
        boolean multipartContent = ServletFileUpload.isMultipartContent(req);
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (multipartContent){
            //创建对象解析数据
            ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
            List<FileItem> fileItems = null;
            try {
                fileItems= servletFileUpload.parseRequest(req);
                System.out.println(fileItems.toArray());
                fileItems.forEach(fileItem -> {
                    String realName = fileItem.getName();
                    System.out.println(realName);
                    String fileType = realName.substring(realName.lastIndexOf("."));
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    String newName = uuid+fileType;
                    try {
                        fileItem.write(new File("F:/carupload/cars/"+newName));
                        map.put("code",0);
                        map.put("msg","");
                        HashMap<String, Object> dataMap = new HashMap<>();
                        //这里的src后面跟的路径可以在项目发布的时候进行指定,即upload文件夹资源对应的发布名称
                        dataMap.put("src","/carupload/cars/"+newName);
                        map.put("data",dataMap);
                        map.put("imagePath",newName);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }

                });
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
        }
        //输出数据
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(map));
        writer.close();

    }

    private void downloadShowFile(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
    }
}
